package com.xauto.pioneer.appuser.activity.mine;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.OrderAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.OrderInfo;
import com.xauto.pioneer.appuser.model.PageInfo;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import de.greenrobot.event.EventBus;

/***
 * 订单列表 页面
 * @author Lemon Thu 9 Mar 11:34
 */
public class OrderActivity extends BaseActivity {

    private PromptDialog promptDialog;
    @Bind(R.id.topBar) TopBar topBar;
    @Bind(R.id.list) PullToRefreshListView pullToRefreshListView;
    private ListView list;
    private OrderAdapter adapter;
    private List<OrderInfo> orderInfoList;
    @Override
    protected int getContentView() {
        return R.layout.ac_list_no_refresh;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.mine_order);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout), getString(R.string.dialogpic_notice),new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        list = pullToRefreshListView.getRefreshableView();
        list.setVerticalScrollBarEnabled(false);
        list.setDividerHeight(3);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //当前点击订单
                OrderInfo currentOrderInfo =  adapter.getItem(position-1);
                OrderDetailActivity.startAction(mContext,currentOrderInfo.car_order_no);
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //刷新
                page = 1;
                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    load();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                }
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new OrderAdapter(this, R.layout.item_order);
        list.setAdapter(adapter);
        page = 1;
        lHandler = new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                pullToRefreshListView.onRefreshComplete();
                JSONArray jsonArray = jsonStatus.data.optJSONArray("car_order_list");
                isMore = jsonStatus.pageInfo.more == 1 ? true : false;
                if(jsonArray != null && jsonArray.length() > 0) {
                    orderInfoList = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        orderInfoList.add(new OrderInfo().parse(jsonArray.optJSONObject(i)));
                    }
                    refreshList();
                }else {
                    if (page == 1)showNoDataView();
                }
            }
        };
        load();
    }
    void refreshList() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (page == 1){adapter.getDataList().clear();}
                adapter.getDataList().addAll(orderInfoList);
                adapter.notifyDataSetChanged();
            }
        });
    }
    private void showNoDataView(){
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_listview_empty, null);
        ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("暂无订单");
        ((ViewGroup)list.getParent()).addView(emptyView);
        list.setEmptyView(emptyView);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    void load() {
        new LHttpLib().getCarOrderList(mContext,memberId, new PageInfo(page, Constants.COUNT), lHandler);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //反注册EventBus
        EventBus.getDefault().unregister(this);
    }
    /**
     * 接受支付成功的通知信息
     * @param paymentCode
     */
    public void onEventMainThread(String paymentCode){
        switch (paymentCode){
            case "success":
                //刷新
                page = 1;
                load();
                break;
        }
    }
}
